﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data;

public partial class UserHistory : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        MembershipUser user = Membership.GetUser();

        if (user != null)
        {

            DVDTableAdapters.DataTable4TableAdapter ta = new DVDTableAdapters.DataTable4TableAdapter();

            //GRIDVIEW 1 = Movies not yet commented by user
            GridView1.DataSource = ta.GetMoviesNotCommentedByUser((Guid)user.ProviderUserKey);
            GridView1.DataBind();

            //GRIDVIEW 2 = Movies commented by user
            DVDTableAdapters.DataTable7TableAdapter ta5 = new DVDTableAdapters.DataTable7TableAdapter();

            GridView2.DataSource = ta5.GetData((Guid)user.ProviderUserKey);
            GridView2.DataBind();


            DVDTableAdapters.DataTable5TableAdapter ta1 = new DVDTableAdapters.DataTable5TableAdapter();
            DataTable dt= ta1.GetNotRatedMOvies((Guid)user.ProviderUserKey);

            try
            {
                DropDownList1.DataSource = dt;
                DropDownList1.DataValueField = "MovieID";
                DropDownList1.DataTextField = "Title";
                DropDownList1.DataBind();

                DVDTableAdapters.RatingTableAdapter ta2 = new DVDTableAdapters.RatingTableAdapter();
                DataTable dt1 = ta2.GetRating(); }
            catch 
            { }
            
        }
        user = null;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        MembershipUser user = Membership.GetUser();

        if (user != null)
        {
            int MovieID = Convert.ToInt32(DropDownList1.SelectedValue);
            double rating = Convert.ToDouble(DropDownList2.SelectedValue);

            DVDTableAdapters.MovieTableAdapter ta = new DVDTableAdapters.MovieTableAdapter();
            double actualRating = (double)ta.GetRate(MovieID);
            int ratingCounter = (int)ta.GetRatingCounter(MovieID);

            ratingCounter += 1;
            rating += actualRating;
            rating /= ratingCounter;

            decimal rate = Convert.ToDecimal(rating);
            
            

            DVDTableAdapters.MovieTableAdapter ta1 = new DVDTableAdapters.MovieTableAdapter();
            ta1.UpdateRating(rate, ratingCounter, MovieID);
            DVDTableAdapters.UserhistoryMovieTableAdapter ta2 = new DVDTableAdapters.UserhistoryMovieTableAdapter();
            ta2.UpdateRating(true, (Guid)user.ProviderUserKey);
        }

        user = null;    

    }
}